/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package damas.gui;

import damas.logicaYcontroladores.DamasLogica;
import damas.modelo.DamasModelo;
import java.awt.event.ActionListener;
import javax.swing.JButton;

/**
 * @author Sim�n Emmanuel Guti�rrez Brida
 * @version 0.1
 */
public class DamasPreferencias extends javax.swing.JFrame implements ActionListener
{
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private DamasLogica logica;

    /**
     * Creates new form DamasPreferencias
     */
    public DamasPreferencias()
    {
        initComponents();
    }

    public void definirLogica(DamasLogica logica)
    {
        this.logica = logica;
    }

    public void start()
    {
        this.color1.setText(this.logica.obtenerColorJugador(0) == DamasModelo.ROJA ? "ROJAS" : "NEGRAS");
        this.color2.setText(this.logica.obtenerColorJugador(1) == DamasModelo.ROJA ? "ROJAS" : "NEGRAS");
        this.nombre1.setText(this.logica.obtenerNombreJugador(0));
        this.nombre2.setText(this.logica.obtenerNombreJugador(1));
        this.inteligencia1.setSelectedIndex(this.logica.obtenerCerebroJugador(0) ? 1 : 0);
        this.inteligencia2.setSelectedIndex(this.logica.obtenerCerebroJugador(1) ? 1 : 0);
        this.setVisible(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jPanel1 = new javax.swing.JPanel();
        nombre1Label = new javax.swing.JLabel();
        inteligencia1Label = new javax.swing.JLabel();
        color1Label = new javax.swing.JLabel();
        nombre1 = new javax.swing.JTextField();
        inteligencia1 = new javax.swing.JComboBox();
        color1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        nombre2Label = new javax.swing.JLabel();
        inteligencia2Label = new javax.swing.JLabel();
        color2Label = new javax.swing.JLabel();
        nombre2 = new javax.swing.JTextField();
        inteligencia2 = new javax.swing.JComboBox();
        color2 = new javax.swing.JButton();
        salvar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Preferencias jugador 1"));

        nombre1Label.setText("nombre");

        inteligencia1Label.setText("inteligencia");

        color1Label.setText("color");

        nombre1.setText("XXX");
        nombre1.setMaximumSize(new java.awt.Dimension(24, 20));
        nombre1.setMinimumSize(new java.awt.Dimension(24, 20));

        inteligencia1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Humano", "IA" }));

        color1.setText("jButton1");
        color1.addActionListener(this);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(inteligencia1Label)
                                .addComponent(nombre1Label)
                                .addComponent(color1Label))
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nombre1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(inteligencia1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(color1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                      .addGap(0, 10, 10))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(nombre1Label)
                                .addComponent(nombre1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGap(18, 18, 18)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(inteligencia1Label)
                                .addComponent(inteligencia1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGap(18, 18, 18)
                      .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(color1Label)
                                .addComponent(color1))
                      .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Preferencias jugador 2"));
        jPanel2.setMaximumSize(new java.awt.Dimension(228, 192));
        jPanel2.setMinimumSize(new java.awt.Dimension(228, 192));
        jPanel2.setPreferredSize(new java.awt.Dimension(228, 192));

        nombre2Label.setText("nombre");

        inteligencia2Label.setText("inteligencia");

        color2Label.setText("color");

        nombre2.setText("XXX");
        nombre2.setMaximumSize(new java.awt.Dimension(24, 20));
        nombre2.setMinimumSize(new java.awt.Dimension(24, 20));

        inteligencia2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Humano", "IA" }));

        color2.setText("jButton1");
        color2.addActionListener(this);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(nombre2Label)
                                .addComponent(inteligencia2Label)
                                .addComponent(color2Label))
                      .addGap(18, 18, 18)
                      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(color2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(nombre2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(inteligencia2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(nombre2Label)
                                .addComponent(nombre2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGap(18, 18, 18)
                      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(inteligencia2Label)
                                .addComponent(inteligencia2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                      .addGap(16, 16, 16)
                      .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(color2Label)
                                .addComponent(color2))
                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        salvar.setText("Salvar");
        salvar.addActionListener(this);

        cancelar.setText("Cancelar");
        cancelar.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                      .addContainerGap(15, Short.MAX_VALUE)
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                          .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                          .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                          .addComponent(cancelar)
                                          .addGap(15, 15, 15)
                                          .addComponent(salvar)))
                      .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                      .addContainerGap()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                      .addGap(18, 18, 18)
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cancelar)
                                .addComponent(salvar))
                      .addContainerGap())
        );

        pack();
    }

    // Code for dispatching events from components to event handlers.

    public void actionPerformed(java.awt.event.ActionEvent evt)
    {
        if (evt.getSource() == color1)
        {
            DamasPreferencias.this.color1ActionPerformed(evt);
        }
        else if (evt.getSource() == color2)
        {
            DamasPreferencias.this.color2ActionPerformed(evt);
        }
        else if (evt.getSource() == salvar)
        {
            DamasPreferencias.this.salvarActionPerformed(evt);
        }
        else if (evt.getSource() == cancelar)
        {
            DamasPreferencias.this.cancelarActionPerformed(evt);
        }
    }// </editor-fold>//GEN-END:initComponents

    private void color1ActionPerformed(java.awt.event.ActionEvent evt)  //GEN-FIRST:event_color1ActionPerformed
    {
        cambiarBotonColor(color1);
        cambiarBotonColor(color2);
    }//GEN-LAST:event_color1ActionPerformed

    private void color2ActionPerformed(java.awt.event.ActionEvent evt)  //GEN-FIRST:event_color2ActionPerformed
    {
        cambiarBotonColor(color2);
        cambiarBotonColor(color1);
    }//GEN-LAST:event_color2ActionPerformed

    private void salvarActionPerformed(java.awt.event.ActionEvent evt)  //GEN-FIRST:event_salvarActionPerformed
    {
        this.logica.cambiarCerebroJugador(0, this.inteligencia1.getSelectedIndex() == 1);
        this.logica.cambiarCerebroJugador(1, this.inteligencia2.getSelectedIndex() == 1);
        this.logica.cambiarNombreJugador(0, this.nombre1.getText());
        this.logica.cambiarNombreJugador(1, this.nombre2.getText());
        this.logica.cambiarColorJugador(0, this.color1.getText().compareToIgnoreCase("rojas") == 0 ? DamasModelo.ROJA : DamasModelo.NEGRA);
        this.logica.cambiarColorJugador(1, this.color2.getText().compareToIgnoreCase("rojas") == 0 ? DamasModelo.ROJA : DamasModelo.NEGRA);
        cerrar();
    }//GEN-LAST:event_salvarActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt)  //GEN-FIRST:event_cancelarActionPerformed
    {
        cerrar();
    }//GEN-LAST:event_cancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try
        {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
            {
                if ("Nimbus".equals(info.getName()))
                {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }
        catch (ClassNotFoundException ex)
        {
            java.util.logging.Logger.getLogger(DamasPreferencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            java.util.logging.Logger.getLogger(DamasPreferencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            java.util.logging.Logger.getLogger(DamasPreferencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        catch (javax.swing.UnsupportedLookAndFeelException ex)
        {
            java.util.logging.Logger.getLogger(DamasPreferencias.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                new DamasPreferencias().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelar;
    private javax.swing.JButton color1;
    private javax.swing.JLabel color1Label;
    private javax.swing.JButton color2;
    private javax.swing.JLabel color2Label;
    private javax.swing.JComboBox inteligencia1;
    private javax.swing.JLabel inteligencia1Label;
    private javax.swing.JComboBox inteligencia2;
    private javax.swing.JLabel inteligencia2Label;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField nombre1;
    private javax.swing.JLabel nombre1Label;
    private javax.swing.JTextField nombre2;
    private javax.swing.JLabel nombre2Label;
    private javax.swing.JButton salvar;
    // End of variables declaration//GEN-END:variables



    private void cambiarBotonColor(JButton button)
    {
        if (button.getText().compareToIgnoreCase("rojas") == 0)
        {
            button.setText("NEGRAS");
        }
        else
        {
            button.setText("ROJAS");
        }
    }

    private void cerrar()
    {
        this.logica.mostrarGUI();
        this.dispose();
    }


}
